<!-- 课程 -->
<script setup>
import { Search, Plus } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { useCourseStore } from '@/stores/index'
const courseStore = useCourseStore()

// 添加课程
const addCourse = ref(false)
const form = ref({
  name: '',
  teacher: '',
  img: ''
})
// 添加验证
const rules = ref({
  name: [{ required: true, message: '请输入课程名称', trigger: 'blur' }],
  teacher: [{ required: true, message: '请输入教师名称', trigger: 'blur' }]
})
// 浏览图片
const handlePictureCardPreview = () => {
  console.log('预览图片')
}
// 移除图片
const handleRemove = () => {
  console.log('移除图片')
}
// 显示新增表单
const AddCourse = () => {
  addCourse.value = true
}
const Submit = () => {
  // 发送给后端
  console.log('图片:' + form.value.img)
}
const cancel = () => {
  addCourse.value = false
}

// 搜索框
const courseValue = ref('')
</script>

<template>
  <div class="course-teacher">
    <el-card>
      <el-container>
        <!-- 头部 -->
        <el-header class="header-teacher">
          <div>
            <el-tabs>
              <el-tab-pane label="我教的课"></el-tab-pane>
            </el-tabs>
          </div>
          <div class="tabs-header">
            <el-button type="primary" round @click="AddCourse">新建课程</el-button>
            <el-input
              v-model="courseValue"
              :suffix-icon="Search"
              class="input-header"
              placeholder="搜索"
            />
          </div>
        </el-header>

        <!-- 主体课程 -->
        <el-main>
          <!-- 课程 -->
          <el-empty description="description" v-if="courseStore.allCourse.length == 0" />
          <ul
            class="course-box"
            @click="courseStore.toCourseDetail(item.courseId)"
            v-for="item in courseStore.allCourse"
            :key="item.courseId"
            v-else
          >
            <li><img :src="item.courseImage" class="course-img" alt="" /></li>
            <li>
              <p class="course-font">{{ item.courseName }}</p>
            </li>
          </ul>

          <!-- 添加课程 -->
          <el-dialog v-model="addCourse" title="新增课程" width="700">
            <el-form :model="form" :rules="rules" label-width="auto" style="max-width: 600px">
              <el-form-item label="课程名称" prop="name">
                <el-input v-model="form.name" />
              </el-form-item>
              <el-form-item label="课程教师" prop="teacher">
                <el-input v-model="form.teacher" />
              </el-form-item>
              <el-form-item label="课程封面">
                <el-upload
                  v-model:file-list="fileList"
                  action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
                  list-type="picture-card"
                  :on-preview="handlePictureCardPreview"
                  :on-remove="handleRemove"
                >
                  <el-icon><Plus /></el-icon>
                </el-upload>

                <el-dialog v-model="dialogVisible">
                  <img w-full :src="dialogImageUrl" alt="Preview Image" />
                </el-dialog>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="Submit">添加</el-button>
                <el-button @click="cancel">返回</el-button>
              </el-form-item>
            </el-form>
          </el-dialog>
        </el-main>
      </el-container>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.course-teacher {
  margin-left: 20px;
  :deep(.el-card__body) {
    height: 100vh;
  }

  // 头部
  .header-teacher {
    height: 80px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 0;

    .tabs-header {
      margin-top: 20px;

      .input-header {
        width: 200px;
        float: right;

        :deep(.el-input__wrapper) {
          border-radius: 50px;
        }
      }
    }
  }

  // 课程
  .course-box {
    margin: 20px;
    display: inline-block;
    flex-direction: column;
    list-style: none;
    gap: 1.25rem;

    .course-img {
      width: 18.75rem;
      height: 12.5rem;
      border-radius: 10px;
    }

    .course-font {
      margin-top: 10px;
    }

    &:hover {
      color: #68beb1;
      cursor: pointer;
      scale: 1.05;
      transition: all 0.3s ease-in-out;
    }
  }
}
</style>
